package com.example.mymvvmdemo.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间格式化工具类
 */
public class SdfUtils {
    @SuppressLint("SimpleDateFormat")
    private static final SimpleDateFormat sdfAfter = new SimpleDateFormat("HH:mm:ss");
    @SuppressLint("SimpleDateFormat")
    private static final SimpleDateFormat sdfBefore = new SimpleDateFormat("yyyy-MM-dd");
    @SuppressLint("SimpleDateFormat")
    private static final SimpleDateFormat sdfAll = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public static String formatAfter(Object timer) {
        if (timer instanceof Date)
            return sdfAfter.format((Date) timer);
        if (timer instanceof String)
            return sdfAfter.format((String) timer);
        if (timer instanceof Long)
            return sdfAfter.format((Long) timer);
        if (timer instanceof Integer)
            return sdfAfter.format(timer);
        return null;
    }

    public static String formatBefore(Object timer) {
        if (timer instanceof Date)
            return sdfBefore.format((Date) timer);
        if (timer instanceof String)
            return sdfBefore.format((String) timer);
        if (timer instanceof Long)
            return sdfBefore.format((Long) timer);
        if (timer instanceof Integer)
            return sdfBefore.format((Integer) timer);
        return null;
    }

    public static String formatBeforeAll(Object timer) {
        String str = "";
        if (timer instanceof Date)
            str = sdfBefore.format((Date) timer);
        if (timer instanceof String)
            str = sdfBefore.format((String) timer);
        if (timer instanceof Long)
            str = sdfBefore.format((Long) timer);
        if (timer instanceof Integer)
            str = sdfBefore.format((Integer) timer);

        String[] split = str.split("-");
        int year = Integer.parseInt(split[0]);
        int month = Integer.parseInt(split[1]);
        int day = Integer.parseInt(split[2]);
        StringBuffer sb = new StringBuffer();
        sb.append(year);
        sb.append("-");
        if (month < 10) {
            sb.append("0");
        }
        sb.append(month);
        sb.append("-");
        if (day < 10) {
            sb.append("0");
        }
        sb.append(day);

        return sb.toString();
    }

    public static String formatAll(Object timer) {
        try {
            if (timer instanceof Date)
                return sdfAll.format((Date) timer);
            if (timer instanceof String)
                return sdfAll.format((String) timer);
            if (timer instanceof Long)
                return sdfAll.format((Long) timer);
            if (timer instanceof Integer)
                return sdfAll.format((Integer) timer);
            if (timer instanceof Double)
                return sdfAll.format((Double) timer);
        } catch (IllegalArgumentException e) {
        }
        return null;
    }

    public static Date parseAfter(String timer) {
        try {
            return sdfAfter.parse(timer);
        } catch (ParseException e) {
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    public static Date parseBefore(String timer) {
        try {
            return sdfBefore.parse(timer);
        } catch (ParseException e) {
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    public static Date parseAll(String timer) {
        try {
            return sdfAll.parse(timer);
        } catch (ParseException e) {
            LogUtils.e(e.getMessage());
        }
        return null;
    }

    /**
     * 获取月初 yyyy-mm-dd
     *
     * @param calendar 当前calendar
     */
    public static String getMonthStart(Calendar calendar) {
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1); //设置为1号,当前日期既为本月第一天
        return formatBefore(calendar.getTime());
    }

    /**
     * 获取月末 yyyy-mm-dd
     *
     * @param calendar 当前calendar
     */
    public static String getMonthEnd(Calendar calendar) {
        calendar.add(Calendar.MONTH, +1); //月份+1
        calendar.set(Calendar.DAY_OF_MONTH, 0); //日期-1,当前日期既为本月最后一天
        return formatBefore(calendar.getTime());
    }
}
